/*
 * CollectionModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.request.CollectGoodsRequest;
import com.yidiandao.business.data.request.CollectShopRequest;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.response.CollectionGoodsResponse;
import com.yidiandao.business.data.response.CollectionShopResponse;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CollectGoodsListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CollectShopListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** 我的 收藏 */
public class CollectionModel implements ICollectionModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void getCollectionGoodsData(
            String tag, int page, final OnGetCollectionGoodsListener listener) {
        PagerRequest request = new PagerRequest();
        request.setUrl(Urls.COLLECTION_GOODS_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(page);
        mNetBiz.sendGetRequest(request, CollectionGoodsResponse.class,
                new NetListener<CollectionGoodsResponse>() {
                    @Override
                    public void onSuccess(CollectionGoodsResponse collectionGoodsResponse) {

                        List<Goods> goodsList = collectionGoodsResponse.getData().getGoodsList();
                        int total = collectionGoodsResponse.getPagination().getTotal();
                        ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                        if (goodsList != null && goodsList.size() > 0) {
                            //解析数据
                            for (int i = 0; i < goodsList.size(); i++) {

                                displayListItems.add(new CollectGoodsListItem(goodsList.get(i)));
                            }
                        }
                        if (listener != null) {
                            listener.onGetGoodsListSuccess(displayListItems, total);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void getCollectionShopData(
            String tag, int page, final OnGetCollectionShopListener listener) {
        PagerRequest request = new PagerRequest();
        request.setUrl(Urls.COLLECTION_SHOP_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(page);
        mNetBiz.sendGetRequest(request, CollectionShopResponse.class,
                new NetListener<CollectionShopResponse>() {
                    @Override
                    public void onSuccess(CollectionShopResponse collectionShopResponse) {

                        List<Shop> goodsList = collectionShopResponse.getData().getShopList();
                        int total = collectionShopResponse.getPagination().getTotal();

                        ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                        if (goodsList != null && goodsList.size() > 0) {
                            //解析数据
                            for (int i = 0; i < goodsList.size(); i++) {
                                displayListItems.add(new CollectShopListItem(goodsList.get(i)));
                            }
                        }
                        if (listener != null) {
                            listener.onGetGoodsListSuccess(displayListItems, total);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void collectionGoods(
            String tag, String goods_id, int type, final OnAccessNetListener listener) {
        CollectGoodsRequest request = new CollectGoodsRequest();
        request.setTag(tag);
        request.setType(type);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.COLLECT_GOODS);
        request.setGoodsId(goods_id);
        mNetBiz.sendGetRequest(request, BaseResponse.class,
                new NetListener<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse baseResponse) {

                        if (listener != null) {
                            listener.onSuccess();
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void collectionShop(
            String tag, String supplier_id, int type, final OnAccessNetListener listener) {
        CollectShopRequest request = new CollectShopRequest();
        request.setTag(tag);
        request.setType(type);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.COLLECT_SHOP);
        request.setSupplierId(supplier_id);
        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }
}
